package com.yunsc.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.store.dao.YunscDeliveryRuleDao;
import com.yunsc.store.dto.StoreDeliveryDTO;
import com.yunsc.store.entity.YunscDeliveryRuleEntity;
import com.yunsc.store.service.YunscDeliveryRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service("yunscDeliveryRuleService")
public class YunscDeliveryRuleServiceImpl extends ServiceImpl<YunscDeliveryRuleDao, YunscDeliveryRuleEntity> implements YunscDeliveryRuleService {


    // @Override
    // public List<StoreDeliveryDTO.RulesForm> queryRuleList(Integer deliveryId) {
    //     LambdaQueryWrapper<YunscDeliveryRuleEntity> eq = new LambdaQueryWrapper<YunscDeliveryRuleEntity>()
    //             .eq(YunscDeliveryRuleEntity::getDeliveryId, deliveryId);
    //     List<YunscDeliveryRuleEntity> deliveryRuleEntities = this.list(eq);
    //     if (CollectionUtils.isEmpty(deliveryRuleEntities)){
    //         return Collections.emptyList();
    //     }
    //     List<StoreDeliveryDTO.RulesForm> collect = deliveryRuleEntities.stream().map(deliveryRule -> {
    //         StoreDeliveryDTO.RulesForm rulesForm = new StoreDeliveryDTO.RulesForm();
    //         BeanUtils.copyProperties(deliveryRule, rulesForm);
    //         rulesForm.setFirst(deliveryRule.getFirst());
    //         rulesForm.setRegion(JSON.parseObject(deliveryRule.getRegion(), List.class));
    //         List<StoreDeliveryDTO.RegionTextForm> regionTextFormList = new ArrayList<>();
    //         List list = JSON.parseObject(deliveryRule.getRegionText(), List.class);
    //         list.forEach(text -> {
    //             StoreDeliveryDTO.RegionTextForm regionTextForm = JSON.parseObject(text.toString(), StoreDeliveryDTO.RegionTextForm.class);
    //             regionTextFormList.add(regionTextForm);
    //         });
    //         rulesForm.setRegionText(regionTextFormList);
    //         return rulesForm;
    //     }).collect(Collectors.toList());
    //     return collect;
    // }
    @Override
    public List<StoreDeliveryDTO.RulesForm> queryRuleList(Integer deliveryId) {
        LambdaQueryWrapper<YunscDeliveryRuleEntity> eq = new LambdaQueryWrapper<YunscDeliveryRuleEntity>()
                .eq(YunscDeliveryRuleEntity::getDeliveryId, deliveryId);
        List<YunscDeliveryRuleEntity> deliveryRuleEntities = this.list(eq);
        if (CollectionUtils.isEmpty(deliveryRuleEntities)) {
            return Collections.emptyList();
        }

        return deliveryRuleEntities.stream().map(deliveryRule -> {
            StoreDeliveryDTO.RulesForm rulesForm = new StoreDeliveryDTO.RulesForm();
            BeanUtils.copyProperties(deliveryRule, rulesForm);
            rulesForm.setFirst(deliveryRule.getFirst());
            rulesForm.setRegion(JSON.parseObject(deliveryRule.getRegion(), List.class));

            List<StoreDeliveryDTO.RegionTextForm> regionTextFormList = JSON.parseArray(deliveryRule.getRegionText(), StoreDeliveryDTO.RegionTextForm.class);
            rulesForm.setRegionText(regionTextFormList);

            return rulesForm;
        }).collect(Collectors.toList());
    }
}